/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.mail;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import myUtils.Utils;
import org.apache.struts2.interceptor.ServletRequestAware;

import po.Mail;
import po.User;
import service.MailService;

/**
 *
 * @author sony
 */
public class ShowMailDetailAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    private MailService mailService;
    private Mail mail;
    private boolean isAuthor;

    @Override
    public String execute()
    {
        Utils.getPreURL(request);

        int id = Integer.valueOf(request.getParameter("mailId"));
        mail = mailService.getMail(id);
        if (ActionContext.getContext().getSession().get("user") != null) {
            int userId = ((User) ActionContext.getContext().getSession().get("user")).getId();
            isAuthor = mailService.isAuthor(id, userId);
            if(userId == mail.getUserByToId().getId()&&mail.getIsread() < 10){
                mail.setIsread(mail.getIsread()+10);
                mailService.updateMail(mail);
            }
        } else {
            isAuthor = false;
        }
        if(!isAuthor)return ERROR;
        else
        return SUCCESS;
    }

    public void setServletRequest(HttpServletRequest arg0) {
        this.request = arg0;
    }


    public boolean isIsAuthor() {
        return isAuthor;
    }

    public void setIsAuthor(boolean isAuthor) {
        this.isAuthor = isAuthor;
    }

    public Mail getMail() {
        return mail;
    }

    public void setMail(Mail mail) {
        this.mail = mail;
    }

    public MailService getMailService() {
        return mailService;
    }

    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }

}
